<?php

namespace api\modules\v2\controllers;
use backend\models\LoginForm;
use Yii;
use yii\rest\Controller;
use yii\helpers\Json;
class UserController extends Controller
{
    public function actionLogin()
    {
        $model = new LoginForm();
        $data = json_decode(Yii::$app->request->getRawBody(),true);
        $model->username = $data['username'];
        $model->password = $data['password'];

        if ($model->login()) {
            return  Json::encode(
                [
                    'result'=>200,
                    'token'=>Yii::$app->user->identity->access_token,
                    'message'=>'',
                ]
            );
        }else{
            return  Json::encode(
                [
                    "result"=>400,
                    "token"=>'',
                    "message"=>current($model->getFirstErrors())
                ]
            );
        }
    }
}
